
package com.lzb.permanentcalendar.dialog;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;

import com.aigestudio.wheelpicker.WheelPicker;
import com.lzb.permanentcalendar.R;
import com.lzb.permanentcalendar.basic.dialog.BaseSmartDialog;
import com.lzb.permanentcalendar.databinding.DialogLunarDateBinding;
import com.lzb.permanentcalendar.databinding.DialogLunarTimeBinding;
import com.nlf.calendar.Lunar;
import com.nlf.calendar.LunarMonth;
import com.nlf.calendar.LunarYear;
import com.nlf.calendar.Solar;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class LunarDateDialog extends BaseSmartDialog<DialogLunarDateBinding> implements View.OnClickListener {

    private List<String> years = new ArrayList<>();
    private List<String> mos = new ArrayList<>();
    private List<String> mos1 = new ArrayList<>();
    private List<String> days = new ArrayList<>();
    private List<String> days1 = new ArrayList<>();

    public LunarDateDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_lunar_date;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvCancel.setOnClickListener(this);
        mDataBinding.tvConfirm.setOnClickListener(this);

        mDataBinding.wheelPicker1.setItemTextSize(50);
        mDataBinding.wheelPicker2.setItemTextSize(50);
        mDataBinding.wheelPicker3.setItemTextSize(50);
        mDataBinding.wheelPicker1.setItemTextColor(Color.parseColor("#80202739"));
        mDataBinding.wheelPicker2.setItemTextColor(Color.parseColor("#80202739"));
        mDataBinding.wheelPicker3.setItemTextColor(Color.parseColor("#80202739"));
        mDataBinding.wheelPicker1.setSelectedItemTextColor(Color.parseColor("#202739"));
        mDataBinding.wheelPicker2.setSelectedItemTextColor(Color.parseColor("#202739"));
        mDataBinding.wheelPicker3.setSelectedItemTextColor(Color.parseColor("#202739"));
        mDataBinding.wheelPicker1.setVisibleItemCount(5);
        mDataBinding.wheelPicker2.setVisibleItemCount(5);
        mDataBinding.wheelPicker3.setVisibleItemCount(5);
        mDataBinding.wheelPicker1.setItemSpace(80);
        mDataBinding.wheelPicker2.setItemSpace(80);
        mDataBinding.wheelPicker3.setItemSpace(80);
        setData();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tvCancel) {
            dismiss();
        } else if (id == R.id.tvConfirm) {
            dismiss();
            if (listener != null) {
                int selectedItemPositionYear = mDataBinding.wheelPicker1.getCurrentItemPosition();
                String yearStr = (String) mDataBinding.wheelPicker1.getData().get(selectedItemPositionYear);
                int year = Integer.parseInt(yearStr.toString().replace("年", ""));
                int selectedItemPosition = mDataBinding.wheelPicker2.getCurrentItemPosition();
                String moS = (String) mDataBinding.wheelPicker2.getData().get(selectedItemPosition);
                int mo = 0;
                if (moS.contains("正月")) {
                    mo = 1;
                } else if (moS.contains("二月")) {
                    mo = 2;
                } else if (moS.contains("三月")) {
                    mo = 3;
                } else if (moS.contains("四月")) {
                    mo = 4;
                } else if (moS.contains("五月")) {
                    mo = 5;
                } else if (moS.contains("六月")) {
                    mo = 6;
                } else if (moS.contains("七月")) {
                    mo = 7;
                } else if (moS.contains("八月")) {
                    mo = 8;
                } else if (moS.contains("九月")) {
                    mo = 9;
                } else if (moS.contains("十月")) {
                    mo = 10;
                } else if (moS.contains("冬月")) {
                    mo = 11;
                } else if (moS.contains("腊月")) {
                    mo = 12;
                }
                int selectedItemPositionDay = mDataBinding.wheelPicker3.getCurrentItemPosition();
                String dayStr = (String) mDataBinding.wheelPicker3.getData().get(selectedItemPositionDay);
                int day = 0;
                if (dayStr.equals("初一")) {
                    day = 1;
                } else if (dayStr.equals("初二")) {
                    day = 2;
                } else if (dayStr.equals("初三")) {
                    day = 3;
                } else if (dayStr.equals("初四")) {
                    day = 4;
                } else if (dayStr.equals("初五")) {
                    day = 5;
                } else if (dayStr.equals("初六")) {
                    day = 6;
                } else if (dayStr.equals("初七")) {
                    day = 7;
                } else if (dayStr.equals("初八")) {
                    day = 8;
                } else if (dayStr.equals("初九")) {
                    day = 9;
                } else if (dayStr.equals("初十")) {
                    day = 10;
                } else if (dayStr.equals("十一")) {
                    day = 11;
                } else if (dayStr.equals("十二")) {
                    day = 12;
                } else if (dayStr.equals("十三")) {
                    day = 13;
                } else if (dayStr.equals("十四")) {
                    day = 14;
                } else if (dayStr.equals("十五")) {
                    day = 15;
                } else if (dayStr.equals("十六")) {
                    day = 16;
                } else if (dayStr.equals("十七")) {
                    day = 17;
                } else if (dayStr.equals("十八")) {
                    day = 18;
                } else if (dayStr.equals("十九")) {
                    day = 19;
                } else if (dayStr.equals("二十")) {
                    day = 20;
                } else if (dayStr.equals("廿一")) {
                    day = 21;
                } else if (dayStr.equals("廿二")) {
                    day = 22;
                } else if (dayStr.equals("廿三")) {
                    day = 23;
                } else if (dayStr.equals("廿四")) {
                    day = 24;
                } else if (dayStr.equals("廿五")) {
                    day = 25;
                } else if (dayStr.equals("廿六")) {
                    day = 26;
                } else if (dayStr.equals("廿七")) {
                    day = 27;
                } else if (dayStr.equals("廿八")) {
                    day = 28;
                } else if (dayStr.equals("廿九")) {
                    day = 29;
                } else if (dayStr.equals("三十")) {
                    day = 30;
                }
                listener.clickConfirm(year, mo, day);
            }
        }
    }

    private void setData() {
        years.clear();
        Solar solar = Solar.fromDate(new Date());
        int yearIndex = 0;
        for (int i = 1900; i <= 2100; i++) {
            years.add(i + "年");
        }
        for (int i = 0; i < years.size(); i++) {
            if (years.get(i).equals(solar.getYear() + "年")) {
                yearIndex = i;
            }
        }
        mDataBinding.wheelPicker1.setData(years);
        int finalYearIndex = yearIndex;
        mDataBinding.wheelPicker1.post(new Runnable() {
            @Override
            public void run() {
                mDataBinding.wheelPicker1.setSelectedItemPosition(finalYearIndex);
            }
        });
        mos1.clear();
        days1.clear();
        setTyep1Date(solar.getYear());

        Lunar lunar = new Lunar();

        int moIndex = 0;

        for (int i = 0; i < mos1.size(); i++) {
            if (mos1.get(i).equals(lunar.getMonthInChinese() + "月")) {
                moIndex = i;
                break;
            }
        }

        mDataBinding.wheelPicker2.setData(mos1);

        int finalMoIndex = moIndex;
        mDataBinding.wheelPicker2.post(new Runnable() {
            @Override
            public void run() {
                mDataBinding.wheelPicker2.setSelectedItemPosition(finalMoIndex);
            }
        });

        mDataBinding.wheelPicker3.post(new Runnable() {
            @Override
            public void run() {
                mDataBinding.wheelPicker3.setSelectedItemPosition(lunar.getDay() - 1);
            }
        });

        mDataBinding.wheelPicker1.setOnItemSelectedListener(new WheelPicker.OnItemSelectedListener() {
            @Override
            public void onItemSelected(WheelPicker picker, Object data, int position) {
                days1.clear();
                if (data.toString().contains("年")) {
                    int year = Integer.parseInt(data.toString().replace("年", ""));
                    setTyep1Date(year);
                }
            }
        });
        mDataBinding.wheelPicker2.setOnItemSelectedListener(new WheelPicker.OnItemSelectedListener() {
            @Override
            public void onItemSelected(WheelPicker picker, Object data, int position) {
                if (data.toString().contains("月")) {
                    int selectedItemPosition = mDataBinding.wheelPicker1.getCurrentItemPosition();
                    String yearStr = (String) mDataBinding.wheelPicker1.getData().get(selectedItemPosition);
                    int year = Integer.parseInt(yearStr.toString().replace("年", ""));
                    setTyep1Date(year);
                }
            }
        });

    }

    private void setTyep1Date(int year) {
        mos1.clear();
        days1.clear();

        LunarYear lunarYear = LunarYear.fromYear(year);
        List<LunarMonth> monthsInYear = lunarYear.getMonthsInYear();
        for (int i = 0; i < monthsInYear.size(); i++) {
            String string = monthsInYear.get(i).toString();
            String yearStr = string.toString().replace("年", ",");
            String moStr = yearStr.replace("(", ",");
            String dayStr = moStr.replace("天", ",");
            String[] split = dayStr.split(",");
            mos1.add(split[1]);
            days1.add(split[2]);
        }

        int currentItemPosition = mDataBinding.wheelPicker2.getCurrentItemPosition();
        int daySize = 0;
        for (int i = 0; i < days1.size(); i++) {
            if (currentItemPosition == i) {
                daySize = Integer.parseInt(days1.get(i));
            }
        }
        days1.clear();
        for (int i = 1; i <= daySize; i++) {
            if (i == 1) {
                days1.add("初一");
            } else if (i == 2) {
                days1.add("初二");
            } else if (i == 3) {
                days1.add("初三");
            } else if (i == 4) {
                days1.add("初四");
            } else if (i == 5) {
                days1.add("初五");
            } else if (i == 6) {
                days1.add("初六");
            } else if (i == 7) {
                days1.add("初七");
            } else if (i == 8) {
                days1.add("初八");
            } else if (i == 9) {
                days1.add("初九");
            } else if (i == 10) {
                days1.add("初十");
            } else if (i == 11) {
                days1.add("十一");
            } else if (i == 12) {
                days1.add("十二");
            } else if (i == 13) {
                days1.add("十三");
            } else if (i == 14) {
                days1.add("十四");
            } else if (i == 15) {
                days1.add("十五");
            } else if (i == 16) {
                days1.add("十六");
            } else if (i == 17) {
                days1.add("十七");
            } else if (i == 18) {
                days1.add("十八");
            } else if (i == 19) {
                days1.add("十九");
            } else if (i == 20) {
                days1.add("二十");
            } else if (i == 21) {
                days1.add("廿一");
            } else if (i == 22) {
                days1.add("廿二");
            } else if (i == 23) {
                days1.add("廿三");
            } else if (i == 24) {
                days1.add("廿四");
            } else if (i == 25) {
                days1.add("廿五");
            } else if (i == 26) {
                days1.add("廿六");
            } else if (i == 27) {
                days1.add("廿七");
            } else if (i == 28) {
                days1.add("廿八");
            } else if (i == 29) {
                days1.add("廿九");
            } else if (i == 30) {
                days1.add("三十");
            }
        }
        mDataBinding.wheelPicker3.setData(days1);
    }

    private void setDays(int daysOfMonth) {
        days.clear();
        for (int i = 1; i <= daysOfMonth; i++) {
            days.add(i + "日");
        }
        mDataBinding.wheelPicker3.setData(days);
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickConfirm(int year, int mo, int days
        );
    }
}
